<?php
/**
 * This class will set the components of reform. Also functions used by reform.
 *
 * @author Redouane Benkaddour <redouane@indago.nl>
 * @version 0.5.2
 * @package reform
 * @subpackage general_components
 */
 
class general_components {
	/**
	* returns an array wich will be returned to the user
	* @var array
	*/
	public $component = array();
	
	/**
	* An array with all attribute names and values of a form object
	* @var array
	*/
	public $attribute = array();
	
	/**
    * check the sanity of the value submitted or predefined and set the error message in the component array.
    *
    * @param string $regex
    * @param string $error
    * @param boolean $pre_check
    * @return boolean|integer
    */
	public function checkSanity($regex, $error, $immediately = false){
	    if($immediately == true && isset($this->attribute['value'])){
	        if(!preg_match("/".$regex."/", $this->attribute['value'])){
	  			$this->setError($error);
	  			return 1;
	  		}
	  		else{
	  			return 0;
	  		}
	    }elseif(isset($_REQUEST[$this->attribute['name']])){
			if(!preg_match("/".$regex."/", $_REQUEST[$this->attribute['name']])){
	  			$this->setError($error);
	  			return 1;
	  		}
	  		else{
	  			return 0;
	  		}
	  	}
	}
	 	
    /**
    * set the values of the array attributes
    *
    * @param array $attributes
    * @return void
    */
	public function setAttributes($attributes = array()){
    	if(count($attributes) > 0){
    		foreach($attributes AS $attribute => $value){
    				$this->attribute[$attribute] = $value;
    		}
    	}
	}
	
	/**
    * set the value of the component info
    *
    * @param string $info
    * @return void
    */
	public function setInfo($info = ''){
	    if(strlen($info) > 0){
	        $this->component['info'] = $info;
	    }
	}
	
	/**
    * set the value of the component title
    *
    * @param string $title
    * @return void
    */
	public function setTitle($title = ''){
	    if(strlen($title) > 0){
	        $this->component['title'] = $title;
	    }
	}
	
	/**
    * set the value of the component error
    *
    * @param string $error
    * @return void
    */
	protected function setError($error = ''){
	    if(strlen($error) > 0){
	        $this->component['error'] = $error;
	    }
	}
}

?>